package com.wiz.weixin.controllor;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.service.WeixinConfigService;
import com.wiz.weixin.to.WeixinConfigTO;

/**
 * 优惠
 * @author xxy
 */
@Controller
@RequestMapping("/discount")
@SuppressWarnings("all")
public class DiscountControllor {
	
	@Resource
	private StationService stationService;
	@Resource
	private WeixinConfigService weixinConfigService;
	@Resource
	private TencentManager tencentManager;
	
	/**
	 * 充值优惠
	 * @param request
	 * @param response
	 * @param session
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listSaleInfo")
	public String listSaleInfo(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
			session.setAttribute("openid", openId);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "未找到微信关联用户");
			return "/error";
		}
		//查询客户的所有会员
		List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
		if(hqIds == null || hqIds.size()<= 0){
			hqIds =  stationService.getHQIds(openId);
			session.setAttribute("hqIds", hqIds);
		}
		if(hqIds == null || hqIds.size()<= 0){
			model.addAttribute("msg", "对不起，您还未注册，请扫描加油站的二维码进行注册");
			return "/error";
		}
		Map<String,List<WeixinConfigTO>> map = weixinConfigService.listSaleInfo(hqIds);
		model.addAttribute("map", map);
		return "/discount/listSaleInfo";
	}
	
	/**
	 * 积分活动
	 * @param request
	 * @param response
	 * @param session
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/activity")
	public String listPointInfo(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
			session.setAttribute("openid", openId);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "未找到微信关联用户");
			return "/error";
		}
		//查询客户的所有会员
		List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
		if(hqIds == null || hqIds.size()<= 0){
			hqIds = stationService.getHQIds(openId);
			session.setAttribute("hqIds", hqIds);
		}
		if(hqIds == null || hqIds.size()<= 0){
			model.addAttribute("msg", "对不起，您还未注册，请扫描加油站的二维码进行注册");
			return "/error";
		}
		Map<String,List<WeixinConfigTO>> map = weixinConfigService.listPointInfo(hqIds);
		model.addAttribute("map", map);
		return "/discount/activity";
		
	}
}
